我有卡的应用程序。在一个标签我需要把数据(串)行。要做到这一点,我选择tableLayout但是当我想用一个文本菜单上的排它不起作用。
I have an application with tabs. In one tab I need to put data (strings) in rows. To do so I chose tableLayout but when I wanted to use a contextmenu on its rows it doesn't work.
我可以显示文本菜单onLongClick但问题是,我不能让关于选择要编辑的行或删除所选行的信息。然后我读的讨论中使用的ListView比tablelayout更好,如果我们有很多行。但我看到的例子延伸listactivity但我并不想这样做。
I can show the contextmenu onLongClick but the problem is that I can't get the info about the selected row to edit or delete the selected row. Then I read in a discussion that using listView is better than tablelayout if we have many rows. But the examples I saw extend listactivity but I don't want to do this.
所以,当我尝试工作的一个ListView控件,并且不延长listactivity我不知道怎么做,我的意思是,我从来没有使用过的ListView之前,所以我尝试,我发现在互联网上了解其不同的例子,但它不工作。这是我做了迄今为止对于ListView:
So when I try working on a listView without extending listactivity I don't know how to do it what I mean is that I've never used listView before so I try different examples I found on the internet to understand it but it's not working. Here's what I did so far for the listView:
String [] items=getRessources().getStringArray(R.arra.resolution);
//Resolution is an array of strings
ListView lv=(ListeView) findViewById(R.id.listView);
v.setAdapter(new ArrayAdapter<string>(this, android.R.layout.simple_list_item_1, items);
当我编译它,我与我在里面数组元素的列表,但首先,我想改变的文字,我不能的颜色。其次我要动态地添加行,我不知道该怎么做或者列表。我想我必须使用一个适配器来做到这一点,但我不知道怎么办。 是否有人可以指导我通过此。我只是想知道如何进入我的列表附加到适配器which'll允许我动态地添加行,添加文本菜单等。
When I compile it I get a list with elements of my array in it but first, I want to change the color of text which I can't. And secondly I want to add rows dynamically to the list which I don't know how to do either. I think I have to use an adapter to do it but I don't know how. Can someone please guide me through this. I just want to know how to attach my list to an adapter which'll allow me to dynamically add rows, add contextMenu etc.
主要活动类:
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
public class SelectedActivity extends Activity {
private SelectedAdapter selectedAdapter;
private ArrayList<String> list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.selected_example);
// populate the model - a simple a list
list = new ArrayList<String>();
list.add("Apple");
list.add("Orange");
list.add("Grape");
list.add("Grape1");
list.add("Grape2");
list.add("Grape3");
list.add("Grape4");
list.add("Grape5");
list.add("Grape6");
// create our SelectedAdapter
selectedAdapter = new SelectedAdapter(this,0,list);
selectedAdapter.setNotifyOnChange(true);
ListView listview = (ListView) findViewById(R.id.listExample);
listview.setAdapter(selectedAdapter);
listview.setOnItemClickListener(new OnItemClickListener() {
//@Override
public void onItemClick(AdapterView arg0, View view,
int position, long id) {
// user clicked a list item, make it "selected"
selectedAdapter.setSelectedPosition(position);
}
});
}
适配器类:
import java.util.List;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;
public class SelectedAdapter extends ArrayAdapter{
// used to keep selected position in ListView
private int selectedPos = -1; // init value for not-selected
public SelectedAdapter(Context context, int textViewResourceId,
List objects) {
super(context, textViewResourceId, objects);
}
public void setSelectedPosition(int pos){
selectedPos = pos;
// inform the view of this change
notifyDataSetChanged();
}
public int getSelectedPosition(){
return selectedPos;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
// only inflate the view if it's null
// if (v == null) {
LayoutInflater vi = (LayoutInflater)this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.selected_row, null);
// }
// get text view
TextView label = (TextView)v.findViewById(R.id.txtExample);
Button btn=(Button)v.findViewById(R.id.btn1);
// change the row color based on selected state
if(selectedPos == position){
label.setBackgroundColor(Color.CYAN);
btn.setBackgroundResource(R.drawable.next);
}
else{
label.setBackgroundColor(Color.WHITE);
}
label.setText(this.getItem(position).toString());
return(v);
}
}
下一篇:外部配置太大,此过程太大、过程