我正在制作一个项目,在其中我在多列列表视图中显示数据库的内容。现在,我还想添加搜索功能。搜索似乎工作正常,但它显示两次行。我不知道造成这种情况的原因。我认为它从名称显示一次,从ticketID显示一次。我如何才能根据第一列过滤结果,即名称,而不是任何其他...请帮助......我非常坚持这个问题。
public class MainActivity extends Activity implements OnRefreshListener,OnQueryTextListener{
GetDataFromDB getdb = new GetDataFromDB();
String data = "";
int flag=0;
String data2="";
SearchView mSearchView;
SimpleAdapter adapter_title,adapter;
HashMap<String, String> map2;
ListView list,list_head;
//ArrayList<Users> users = new ArrayList<Users>();
ArrayList<HashMap<String, String>> users;
ArrayList<HashMap<String, String>> mylist_title;
TextView label;
private SwipeRefreshLayout mSwipeRefreshLayout;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list = (ListView) findViewById(R.id.listView2);
list_head = (ListView) findViewById(R.id.listView1);
mylist_title = new ArrayList<HashMap<String, String>>();
mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_container);
mSwipeRefreshLayout.setOnRefreshListener(this);
// tl = (TableLayout) findViewById(R.id.maintable);
mSearchView = (SearchView) findViewById(R.id.searchView1);
list.setAdapter(adapter);
list.setTextFilterEnabled(true);
setupSearchView();
users = new ArrayList<HashMap<String,String>>();
if (AppStatus.getInstance(this).isOnline()) {
Toast.makeText(this,"Connected",8000).show();
} else {
Toast.makeText(this,"NO INTERNET CONNECTION DETECTED",8000).show();
Log.v("Home", "############################You are not online!!!!");
}
new Thread(new Runnable() {
public void run() {
data = getdb.GetDataFromDB();
System.out.println(data);
runOnUiThread(new Runnable() {
@Override
public void run() {
System.out.println(users);
users = parseJSON(data);
System.out.println(users);
addHeader();
addData();
}
});
}
}).start();
}
@SuppressLint("NewApi")
public ArrayList<HashMap<String, String>>parseJSON(String result) {
try {
JSONArray jArray = new JSONArray(result);
System.out.println("Printing Jarray length--");
System.out.println(jArray.length());
for (int i = 0; i < jArray.length(); i++) {
JSONObject json_data = jArray.getJSONObject(i);
System.out.println(users);
System.out.println(json_data.getString("Name"));
map2 = new HashMap<String, String>();
map2.put("Name", json_data.getString("Name"));
map2.put("TicketID",json_data.getString("TicketID"));
map2.put("Status", json_data.getString("Status"));
System.out.println(map2);
users.add(map2);
System.out.println(users);
System.out.println("********CHECKPOINT**********");
System.out.println(map2);
System.out.println(users);
System.out.println("********CHECKPOINT ENDS**********");
}
} catch (JSONException e) {
Log.e("log_tag", "Error parsing data " + e.toString());
}
return users;
}
void addHeader(){
HashMap<String, String> map1;
map1 = new HashMap<String, String>();
map1.put("Name", "Name");
map1.put("TicketID", "TicketID");
map1.put("Status", "Status");
mylist_title.add(map1);
try {
adapter_title = new SimpleAdapter(this, mylist_title, R.layout.row,
new String[] { "Name", "TicketID", "Status" }, new int[] {
R.id.Name,R.id.TicketID,R.id.Status});
list_head.setAdapter(adapter_title);
} catch (Exception e) {
System.out.println("Exception caught in addHeader");
e.printStackTrace();
}
}
@SuppressWarnings({ "rawtypes", "deprecation" })
public void addData() {
try{
adapter = new SimpleAdapter(this, users, R.layout.row,
new String[] { "Name", "TicketID", "Status" }, new int[] {
R.id.Name, R.id.TicketID, R.id.Status });
list.setAdapter(adapter);
}catch(Exception e){
System.out.println("Exception in Add data method....");
e.printStackTrace();
}
}
private void setupSearchView() {
System.out.println(users);
mSearchView.setIconifiedByDefault(false);
mSearchView.setOnQueryTextListener(this);
mSearchView.setSubmitButtonEnabled(true);
mSearchView.setQueryHint("Search Passengers here....");
}
@SuppressWarnings("deprecation")
@Override
public void onRefresh() {
// TODO Auto-generated method stub
// tl.removeAllViewsInLayout();;
flag=0;
if (AppStatus.getInstance(this).isOnline()) {
Toast.makeText(this,"Connected",8000).show();
Toast.makeText(this, "Updating List", Toast.LENGTH_LONG).show();
} else {
flag=1;
Toast.makeText(this,"NO INTERNET CONNECTION DETECTED",8000).show();
Log.v("Home", "############################You are not online!!!!");
}
mSwipeRefreshLayout.setColorScheme(android.R.color.holo_blue_bright,
android.R.color.holo_red_light,
android.R.color.holo_green_light,
android.R.color.holo_orange_light
);
//final GetDataFromDB db = new GetDataFromDB();
System.out.println("IN REFRESH METHOD");
System.out.println(data);
//data=null;
//data = getdb.GetDataFromDB();
//System.out.println("From onRefresh");
//System.out.println(data);
//System.out.println(users);
// users = parseJSON(data2);
//System.out.println(data2);
//System.out.println(users);
//addData(users);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
System.out.println(users);
// addData(users);
new Thread(new Runnable() {
public void run() {
data = getdb.GetDataFromDB();
System.out.println(data);
runOnUiThread(new Runnable() {
@Override
public void run() {
if(flag!=1){ //i.e internet connnection available
users.clear();
users = parseJSON(data);
System.out.println(users);
// addHeader();
addData();
System.out.println(list);
}
else{
Toast.makeText(getBaseContext(),"Retaining List",8000).show();
users = parseJSON(data);
}
}
});
}
}).start();
mSwipeRefreshLayout.setRefreshing(false);
}
}, 2000);
}
@Override
public boolean onQueryTextSubmit(String query) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
if (TextUtils.isEmpty(newText)) {
System.out.println("IN IF CONDITION FIRST");
System.out.println(users);
list.clearTextFilter();
} else {
System.out.println("IN else CONDITION FIRST");
System.out.println(users);
list.setFilterText(newText.toString());
}
return true;
}
}
<?xml version="1.0" encoding="UTF-8"?>
android:layout_marginRight="18dp" />
<ListView android:id="@+id/listView1" android:layout_width="match_parent"
android:layout_height="wrap_content" android:background="#A3A3C2" />
<android.support.v4.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/swipe_container"
android:layout_width="match_parent" android:layout_height="wrap_content">
<ListView android:id="@+id/listView2" android:layout_width="match_parent"
android:layout_height="wrap_content" android:background="#007A00"
android:backgroundTint="#990000" />
</android.support.v4.widget.SwipeRefreshLayout>