Reputation: 3
I am really new into Android development, so I am sorry if this question may sound funny to some of you. I need to center the items of my ListView Menu just for the sake of aestheticism, but I really don't know how since the code is written only in java without the use of XML.
package com.example.mysqltest;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class Menu extends ListActivity {
String classes[] = {"ProductList", "example1", "example2", "example3", "example4", "example5"};
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(Menu.this, android.R.layout.simple_list_item_1, classes));
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
String local = classes[position];
try {
Class ourClass = Class.forName("com.example.mysqltest." + local);
Intent ourIntent = new Intent(Menu.this, ourClass);
startActivity(ourIntent);
} catch(ClassNotFoundException e){
e.printStackTrace();
}
}
}
Upvotes: 0
Views: 113
Reputation: 3254
You need to create your own Adapter :
public class MyAdapter extends BaseAdapter{
private LayoutInflater inflater;
private List<String> data;
public MyAdapter(Context context, List<String> data){
this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.data = data;
}
public int getSize(){
return data.getSize();
}
public Object getItem(int position){
return data.get(position);
}
public View getView(View convertView, int position, ViewGroup parent){
View v = convertView;
ViewHolder h;
if(v == null){
v = inflater.inflate(R.layout.my_adapter, false);
h = new ViewHolder();
t.tv = (TextView) v.findViewById(R.id.my_textview);
v.setTag();
}else{
h = (ViewHolder) v.getTag();
}
String s = (String) getItem(position);
h.tv.setText(s);
return v;
}
public class ViewHolder{
TextView tv;
}
}
I don't really remember by heart because of the autocompletion in Eclipse, but it should look like this.
Your my_adapter.xml is where you will place your TextView everywhere you want, you will be able to add more, like ImageViews and others TextViews.
And to call it it's simple :
MyAdapter adapter = new MyAdapter(context, myList);
listView.setAdapter(adapter);
Upvotes: 0
Reputation: 481
just write this code before setting adapter,here lv is your listview lv.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
Upvotes: 0
Reputation: 5246
It's not only written in Java, you have to supply an XML layout for the Adapter to inflate. In your case, you've got R.layout.simple_list_item_1
. Where you set it here:
setListAdapter(new ArrayAdapter<String>(Menu.this, android.R.layout.simple_list_item_1, classes));
As you've not posted the content of R.layout.simple_list_item_1
- I have to assume that it's just a TextView
that's contained within it. As such, you can quite easily align text with something like the following:
TextView textView =(TextView)findViewById(R.id.texviewid);
textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
Failing that, you could always set it within the XML of R.layout.simple_list_item_1
and not worry about it.
Hope this helps.
Upvotes: 0
Reputation: 1621
You can change the layout you are passing to your adapter here:
setListAdapter(new ArrayAdapter<String>(Menu.this, android.R.layout.simple_list_item_1, classes));
This piece is the layout of the row:
android.R.layout.simple_list_item_1
Check this answer that explains what it is.
Upvotes: 1