如何从ListView中的单元格中提取文本

时间:2016-08-24 18:39:40

标签: android listview

我在MainActivity中创建了一个Student类和一个ListView。这是MainActivity:

Context context;
ListView listView;
ArrayList<Student> lista;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    lista= new ArrayList<>();
    context=this;
    Student cursant = new Student("Smith","Ben",14);
    Student cursant2 = new Student("White","Jack",21);
    Student cursant3 = new Student("Stewart","Matt",33);
    lista.add(cursant);
    lista.add(cursant2);
    lista.add(cursant3);
    listView=(ListView) findViewById(R.id.listview);
    listView.setAdapter(new CustomAdapter(this,R.layout.activity_main,lista));

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Intent i = new Intent(MainActivity.this,Main2Activity.class);
            String selected =((TextView)view.findViewById(R.id.textViewNume)).getText().toString();
            i.putExtra("name",selected);
            startActivity(i);
        }
    });
}

这是我的CustomAdapter:

public class CustomAdapter extends ArrayAdapter<Student> {
ArrayList<Student> current_objects;
Context context;
private static LayoutInflater inflater=null;
public CustomAdapter(Context context, int resource, ArrayList<Student> objects) {
    super(context, resource, objects);
    this.current_objects=objects;
    inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public int getCount() {
    return current_objects.size();
}

@Override
public Student getItem(int position) {
    return current_objects.get(position);
}

@Override
public long getItemId(int position) {
    return 0;
}

public class Holder{
    TextView tv1,tv2,tv3;
    Button button;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    Holder holder = new Holder();
    final View rowView;
    rowView=inflater.inflate(R.layout.item,null);
    holder.tv1=(TextView) rowView.findViewById(R.id.textViewNume);
    holder.tv2=(TextView) rowView.findViewById(R.id.textViewPrenume);
    holder.tv3=(TextView) rowView.findViewById(R.id.textViewVarsta);

    final Student current_cursant = current_objects.get(position);
    holder.tv1.setText(current_cursant.getNume());
    holder.tv2.setText(current_cursant.getPrenume());
    holder.tv3.setText(String.valueOf(current_cursant.getVarsta()));

    holder.button=(Button) rowView.findViewById(R.id.button);
    holder.button.setTag(position);

    holder.button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Integer index=(Integer)v.getTag();
            current_objects.remove(index.intValue());
            notifyDataSetChanged();
        }
    });

    return rowView;
}

}

我创建了Main2Activity,当我按下listview的一个单元格时,我想显示名称(在Main2Activity中)。我不确定ClickListener。

3 个答案:

答案 0 :(得分:0)

首先尝试

SELECT UM.username
     , GM.GroupID
     , B.Type
     , B.BatchName
     , B.Count
     , B.Date
FROM inno_Profiles.pUserMatch UM
INNER JOIN inno_Profiles.pGroupMatch GM 
   on GM.GroupID = UM.GroupID
INNER JOIN inno_donorsearch.batches  B
   on B.ID = UM.GroupID
GROUP BY UM.username, UM.GroupID

如果这给你正确的结果,那么只需添加替换Toast语句的Intent代码。

答案 1 :(得分:0)

您的onItemClick部分似乎是正确的,所以我猜问题必须是如何在Main2Activity上显示文字。

您已经传递了意图并启动了Main2Activity。

现在你只需要接收意图并展示它。

假设您在Main2Activiy中有一个带有id textView1的textView,那么您将收到如下意图:

Bundle extras = getIntent().getExtras();
        if (extras == null) {
            return;
        }

        String name= extras.getString("name");

        TextView textView = (TextView) 
                findViewById(R.id.textView1);       
        textView.setText(name);

答案 2 :(得分:0)

这对你有用:

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Student student = (Student)parent.getItemAtPosition(position);
        // do whatever you want with student object
    }
});