在ListView中单击时无法更改TextView

时间:2015-12-15 19:44:37

标签: android listview

这是我的onItemClickListener方法

#include <iostream>
#include <algorithm>

using namespace std;

int main (int argc, char** argv) {
  string alt = "LOA";

  // CODE PART 1
  string stringToFind = "AAA";

  string df = "AAAA";

  if (df.find(stringToFind) == string::npos && count(df.begin(), df.end(), 'L') <= 1) {
    cout << df; // this does not print AAAA
  }

  /* CODE PART 2:
    this was an attempt to print out every four length string combination
    of the characters L, O, A where strings with three A's in a row and
    more than one L were excluded.
  */
  for (size_t i = 0; i < 3; i++) {
    char c1 = alt[i];
    for (size_t iT = 0; iT < 3; iT++) {
      char c2 = alt[iT];
      for (size_t itr = 0; itr < 3; itr++) {
        char c3 = alt[itr];
        for (size_t itrI = 0; itrI < 3; itrI++) {
          char c4 = alt[itrI];
          string s = string(&c1)+string(&c2)+string(&c3)+string(&c4);

          if (s.find(stringToFind) == string::npos && count(s.begin(), s.end(), 'L') <= 1) {
            cout << s << endl; // this however, does print out AAAA
          }
        }
      }
    }
  }
  return 0;
}

编辑:这是适配器类

调用适配器的代码:

//list item listener
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
         LinearLayout parent = (LinearLayout) view;
         TextView t = (TextView) parent.findViewById(R.id.grocery_item_name);

         t.setText("Something");
}
adapter = new CustomList(this,R.layout.listitems_improved,Items);

listview.setAdapter(adapter);

当我调用此行时 private class CustomList extends ArrayAdapter{ ArrayList<Item> items; Activity myActivity; LayoutInflater layoutinflater; View myview; public ProductsCustomList(Context context, int resource, ArrayList<item> i) { super(context, resource, p); this.items = i; this.myActivity = (Activity) context; } @Override public View getView(int position, View convertView, ViewGroup parent) { //Inflate the custom row layout in the view layoutinflater = myActivity.getLayoutInflater(); myview = layoutinflater.inflate(R.layout.groceryitems_custom_listview, null); //instantiate the views in custom row layout TextView tv = (TextView) myview.findViewById(R.id.tv); tv.setText(items.get(position).getItemName()); return myview; } } textview在ListView中没有变化,没有任何反应。为什么不在这里唠叨?

4 个答案:

答案 0 :(得分:1)

您可以这样做:

final Adapter yourAdapter = new Adapter(context, items);

listview.setAdapter(yourAdapter);

listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l){

     //change value for item select
     items.get(i).setItemName("yourText");

     yourAdapter.notifyDataSetChanged();
     //if the this line don't refresh the list, you can recreate adapter:
     //listview.setAdapter(new Adapter(context, items); 

}

问候。

答案 1 :(得分:1)

请在Adapter类的getView()方法中尝试:

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
            //Inflate the custom row layout in the view
            layoutinflater = myActivity.getLayoutInflater();
            myview = layoutinflater.inflate(R.layout.groceryitems_custom_listview, null);

            //instantiate the views in custom row layout
            TextView tv = (TextView) myview.findViewById(R.id.tv);
            tv.setText(items.get(position).getItemName());


           myview.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                TextView view = (TextView) v.findViewById(R.id.your_text_id);
                view.setText("text_changed");
            }
        });


            return myview;
        }

答案 2 :(得分:0)

您也可以尝试在侦听器中包含侦听器:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
        //Inflate the custom row layout in the view
        layoutinflater = myActivity.getLayoutInflater();
        myview = layoutinflater.inflate(R.layout.groceryitems_custom_listview, null);

        //instantiate the views in custom row layout
        TextView tv = (TextView) myview.findViewById(R.id.tv);

        tv.setText(items.get(position).getItemName());


        tv.setOnClickListener(new View.OnClickListener(){
                @Override
                public void onClick(View arg0){                 
                    tv.setText("selected");                 
                }               
        });


        return myview;
    }

答案 3 :(得分:0)

listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
         LinearLayout parent = (LinearLayout) view;
         TextView t = (TextView) parent.getChilldId(i).findViewById(R.id.grocery_item_name);

         t.setText("Something");
}