我正在使用ArrayList
将JSON数据解析为ListView,现在我想知道如何将remove duplicate
条目从ArrayList填入ListView
之前。
这是我的 JSON :
{
"data": [
{
"city": "Delhi"
},
{
"city": "Mumbai"
},
{
"city": "Delhi"
},
{
"city": "Mumbai"
}
]
}
MainActivity.java
public class MainActivity extends Activity {
ArrayList<Destination> destinationArrayList;
MainAdapter adapter;
EditText editText;
Destination destination;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText) findViewById(R.id.editLocation);
destinationArrayList = new ArrayList<Destination>();
new JSONAsyncTask().execute("my api link");
ListView listview = (ListView)findViewById(R.id.list);
adapter = new MainAdapter(getApplicationContext(), R.layout.row, destinationArrayList);
listview.setAdapter(adapter);
}
class JSONAsyncTask extends AsyncTask<String, Void, Boolean> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Boolean doInBackground(String... urls) {
try {
....
if (status == 200) {
HttpEntity entity = response.getEntity();
String data = EntityUtils.toString(entity);
JSONObject jsono = new JSONObject(data);
JSONArray jarray = jsono.getJSONArray("data");
for (int i = 0; i < jarray.length(); i++) {
JSONObject object = jarray.getJSONObject(i);
destination = new Destination();
destination.setCity(object.getString("city"));
destinationArrayList.add(destination);
}
return true;
}
......
return false;
}
protected void onPostExecute(Boolean result) {
adapter.notifyDataSetChanged();
}
}
}
答案 0 :(得分:1)
您可以使用HashSet
。如果您使用模型类,则需要创建模型类的arraylist。
ArrayList<Destination> destinationArrayList = new ArrayList<Destination>();
Set<Destination> hashset = new HashSet<>();
hashset.addAll(destinationArrayList);
destinationArrayList.clear();
destinationArrayList.addAll(hashset);
答案 1 :(得分:0)
保留Set<String>
城市名称。当您看到每个城市时,将其添加到集合中,然后仅在set.add(...)
方法返回true时将其添加到列表中(从而表明它已添加到集合中,这意味着它之前不存在,这意味着这是你第一次看到这个城市。)
答案 2 :(得分:0)
您可以将ArrayList传递给HashSet,并且将处理重复的元素。
答案 3 :(得分:0)
如果您使用HashSet
,则会删除重复项:
HashSet<string> hashset = new HashSet<>(destinationArrayList);
hashset
将有一个唯一的列表
答案 4 :(得分:0)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText) findViewById(R.id.editLocation);
destinationArrayList = new ArrayList<Destination>();
new JSONAsyncTask().execute("my api link");
Set<String> hs = new HashSet<>();
hs.addAll(destinationArrayList );
destinationArrayList .clear();
destinationArrayList .addAll(hs);
ListView listview = (ListView)findViewById(R.id.list);
adapter = new MainAdapter(getApplicationContext(), R.layout.row, destinationArrayList);
listview.setAdapter(adapter);
}
答案 5 :(得分:0)
你能试试吗
public static void main( String[] args )
{
List<String> withDuplicatesCities = new ArrayList<String>();
withDuplicatesCities.add( "Dehli" );
withDuplicatesCities.add( "Mumbai" );
withDuplicatesCities.add( "Dehli" );
withDuplicatesCities.add( "Mumbai" );
List<String> withoutDuplicates = removeDuplicates( withDuplicatesCities );
System.out.println( withoutDuplicates );
}
private static List<String> removeDuplicates( List<String> withDuplicatesCities )
{
Set<String> set = new TreeSet<String>();
List<String> newList = new ArrayList<String>();
for( String city : withDuplicatesCities )
{
set.add( city );
}
newList.addAll( set );
return newList;
}
答案 6 :(得分:-1)
使用此
HashSet<String> hs=new HashSet<>(list);