单击行时获取文本内容

时间:2014-02-18 21:56:03

标签: java android android-layout listview layout-inflater

我正在搜索某些方法,以便在单击行时从视图中获取文本

我使用了列表适配器的布局inflater,但我被提起来解决这个问题。 我试图使用:

    public class LastDaysAdapter extends ArrayAdapter<LastDaysHelper>{


    private final Activity activity;
     List<LastDaysHelper> busIDs = new ArrayList<LastDaysHelper>();
     List<LastDaysHelper> Dates = new ArrayList<LastDaysHelper>();
    Context context;

    public LastDaysAdapter(Activity activity,List<LastDaysHelper> list, Context context2, List<LastDaysHelper> list2){
        super(activity, R.layout.activity_last_days,list );
        this.activity=activity;
        this.busIDs=list;
        this.Dates=list2;
        this.context=context2;
    }


    class ViewHolder {
        protected TextView palteNumber;
        protected TextView LastDate;
        protected TableRow row;
        ;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        View view = null;

        if (convertView == null) {

            LayoutInflater inflator = activity.getLayoutInflater();
            view = inflator.inflate(R.layout.rep, null);
            ViewHolder viewHolder = new ViewHolder();


            viewHolder.palteNumber = (TextView) view
                    .findViewById(R.id.TextView1);

            viewHolder.LastDate = (TextView) view
                    .findViewById(R.id.textView2);  



            viewHolder.row = (TableRow) view.findViewById(R.id.TableRow05);
            view.setTag(viewHolder);
            viewHolder.row.setTag(busIDs.get(position));



        } else{
            view = convertView;


            ((ViewHolder) view.getTag()).row.setTag(busIDs
                    .get(position));

        }


        final ViewHolder holder = (ViewHolder) view.getTag();


        holder.palteNumber.setText(busIDs.get(position).getDate());

        holder.LastDate.setText(Dates.get(position).getDate());
        return view;
}
}

编辑我试着用这个:

 public class LastDaysActivity extends ListActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_last_days);
    final Button newFourm = (Button) findViewById(R.id.button1);

    LastDaysAdapter adapter = new LastDaysAdapter(this, getDates(),
            getApplicationContext(), getBusID());

    setListAdapter(adapter);

    newFourm.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = new Intent(LastDaysActivity.this,
                    TabHostActivity.class);
            startActivity(i);

        }
    });

}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    String item = (String) getListAdapter().getItem(position);
    Toast.makeText(this, item + " selected", Toast.LENGTH_LONG).show();
}

public List<LastDaysHelper> getDates() {

    final MySQLiteHelper my = new MySQLiteHelper(LastDaysActivity.this);

    return my.getDates();

}

public List<LastDaysHelper> getBusID() {

    final MySQLiteHelper my = new MySQLiteHelper(LastDaysActivity.this);

    return my.getBuses();

}

}

1 个答案:

答案 0 :(得分:0)

编辑:

我很抱歉。最初你没有包含ListActivity,我在编辑时没有注意到它。

这样做:

1)摆脱setOnClickListner(); 2)在将@Override onListItemClick更改为:

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    TextView myView = (TextView) v.findViewById(R.id.{id_of_your_text_view});
    String text = myView.getText().toString();
    Toast.makeText(this, text + " selected", Toast.LENGTH_LONG).show();
}