Android Persistent ListView回收

时间:2015-06-10 11:20:34

标签: java android android-listview android-adapter

我几天来一直在与这个问题作斗争。在stackoverflow或任何解决方案的任何地方寻找解决方案,尝试所有这些甚至尝试了recyclerView但没有任何帮助。

我的问题是我在listItem上有一个listview和两个textView。所以每当我点击列表项时,一个textView的可见性就是GONE。但是当我向下滚动时,我看到不仅点击了一个已经消失,而且许多其他人也消失了。 继承我的代码

CustomAdapter.java类

package com.example.nara.testtt;

import android.content.Context;
import android.text.Layout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.
import java.util.ArrayList;

public class CustomAdapter extends BaseAdapter {
private ArrayList<String> list = new ArrayList<String>();
private Context context;
public CustomAdapter(Context context){
        this.context = context;
    for(int i = 0; i < 50; i++){
        list.add("item at " + i);
    }
}

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

@Override
public String  getItem(int position) {
    return list.get(position);
}

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    final ViewHolder holder;
    if(convertView == null){
        LayoutInflater inflator = LayoutInflater.from(context);
        convertView = inflator.inflate(R.layout.list_item,null);

        holder = new ViewHolder(convertView);
        convertView.setTag(holder);
    } else{
        holder = (ViewHolder)convertView.getTag();
    }

    holder.tv1.setText(getItem(position));
    holder.tv2.setText(getItem(position));

    return convertView;
}

public static class ViewHolder{
    private TextView tv1;
    private TextView tv2;

    public ViewHolder(View view){
        tv1 = (TextView) view.findViewById(R.id.textView);
        tv2 = (TextView) view.findViewById(R.id.textView2);
    }
}
}

MainActivity.java

package com.example.nara.testtt;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
CustomAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    adapter = new CustomAdapter(this);

    ListView lv = (ListView) findViewById(R.id.listView);
    lv.setAdapter(adapter);
    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            TextView tv = (TextView) view.findViewById(R.id.textView);
            tv.setVisibility(View.GONE);
            adapter.notifyDataSetChanged();
        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

}

3 个答案:

答案 0 :(得分:1)

getView()设置文本更新后,textview的可见性为Visible,因为ListView中的列表项将被回收。

 holder.tv1.setText(getItem(position));
 holder.tv2.setText(getItem(position));
 holder.tv1.setVisibility(View.VISIBLE);
//Your code.....

已更新:

公共类CustomAdapter扩展BaseAdapter {         private ArrayList list = new ArrayList();         私有上下文上下文;

    public CustomAdapter(Context context) {
        this.context = context;
        for (int i = 0; i < 50; i++) {
            list.add(new Bean("item at " + i));
        }
    }

    @Override
    public Bean getItem(int position) {
        return list.get(position);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        //Initialization .......

        Bean item = getItem(position);
        holder.tv1.setText(item.name);
        holder.tv2.setText(item.name);
        holder.tv1.setVisibility(item.visibility);
        return convertView;
    }
    // ViewHolder declaration 
    .......
public static class Bean {
    String name;
    int visibility = View.VISIBLE;

    public Bean(String name) {
        this.name = name;
    }
}

在你的活动的onItemClick上。

public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        TextView tv = (TextView) view.findViewById(R.id.textView);
        Bean bean = Adapter.getItem(position);
        bean.visibility = View.GONE
        tv.setVisibility(View.GONE);
        adapter.notifyDataSetChanged();
    }

答案 1 :(得分:1)

问题是视图在适配器中被回收。因此,当您滚动时,旧视图将被用于新视图。这意味着,如果旧视图的项目的可见性设置为GONE,则新视图中的GONE仍然是holder.tv1.setVisibility(View.VISIBLE); holder.tv1.setText(getItem(position)); holder.tv2.setText(getItem(position)); 。如果您不想保存可见性,可以将它们设置为在适配器中可见:

if(clicked.get(i)) holder.tv1.setVisibility(View.GONE);
else holder.tv1.setVisibility(View.VISIBLE);

或者您将布尔值存储在列表或类似的内容中,并询问是否单击了该项:

#include<stdio.h>
#include<conio.h>

int mobileno(char[]);

int main(){

 char input[20];
 gets(input); 
 printf("%s",input);          
 int x;
 x = mobileno(input);  
printf("%d",x);
switch(x){
          case 0:printf("7255222319");break;
          case 1:printf("9404521113");break;
          case 2:printf("9326513553");break;
          case 3:printf("9845621611");break;
          case 4:printf("4556312390");break;
          case 5:printf("9245178190");break;
          case 6:printf("9214188214");break;
          case 7:printf("7014773244");break;
          case 8:printf("7898888044");break; 

          };    

    getch(); 
    return 0;
}

int mobileno(char s[20]){


    if( s="katrina" )
    {return 1;}else{
    if( s="shahid" ){return 2;}else{
    if( s="ranbir" ){return 3;}else{
    if( s="sharukh" ){return 4;}else{
    if( s="hema" ){return 5;}else{
    if( s="amitabh" ){return 6;}else{
    if( s="shashnk" ){return 7;}else{
    if( s="raj" ){return 8;}else{
    if( s="aishwarya" ){return 0;}else{

        return 9;}}}}}}}}};

    };

答案 2 :(得分:0)

替换

convertView = inflator.inflate(R.layout.list_item,null);

convertView = inflator.inflate(R.layout.list_item,container,false);