如何使用php mysql从数据库到listview的过滤数据?

时间:2016-08-11 16:11:28

标签: php android mysql listview

大家好我有一个Listview会从我的database using php获取详细信息,但I want the poster will see their own posts on the database我使用了用户名的一对多关系,因为它保存在共享偏好中。

这是我的代码。

从数据库获取数据。

<?PHP
include_once("connection.php");

$query = "SELECT * FROM car_category ORDER BY Car_No DESC"; 

$result = mysqli_query($conn, $query);

while($row = mysqli_fetch_assoc($result)){
        $data[] = $row;
}
echo json_encode($data);
?>

我的列表视图。

public class ownerhome extends AppCompatActivity implements AsyncResponse, AdapterView.OnItemClickListener {
final String TAG = this.getClass().getName();
private ArrayList<Cars> carList;
private ListView lvCars;
private FunDapter<Cars> adapter;
SharedPreferences pref;
SharedPreferences.Editor editor;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_ownerhome);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    ImageLoader.getInstance().init(UILConfig.config(ownerhome.this));
    PostResponseAsyncTask taskRead = new PostResponseAsyncTask(ownerhome.this, this);
    taskRead.execute("http://carkila.esy.es/user.php");

    lvCars = (ListView) findViewById(R.id.lvOnwer);



    pref = getSharedPreferences("Login.conf", Context.MODE_PRIVATE);

    Log.d(TAG, pref.getString("username", ""));
    Log.d(TAG, pref.getString("password", ""));

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent in = new Intent(ownerhome.this, InsertActivity.class);
            startActivity(in);
        }
    });

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater findMenuItems = getMenuInflater();
    findMenuItems.inflate(R.menu.menu_main, menu);
    return super.onCreateOptionsMenu(menu);

}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    if(id == R.id.action_logout){
        editor = pref.edit();
        editor.clear();
        editor.commit();
        Intent in = new Intent(ownerhome.this, MainActivity.class);
        startActivity(in);
        finish();
    }
    return super.onOptionsItemSelected(item);
}


@Override
public void processFinish(String s) {
    Log.d(TAG, s);

    carList = new JsonConverter<Cars>().toArrayList(s,Cars.class);

    BindDictionary<Cars> dict = new BindDictionary<Cars>();

    dict.addDynamicImageField(R.id.ivImg, new StringExtractor<Cars>() {
        @Override
        public String getStringValue(Cars product, int position) {
            return product.Image;
        }
    }, new DynamicImageLoader() {
        @Override
        public void loadImage(String url, ImageView imageView) {

            ImageLoader.getInstance().displayImage(url, imageView); // Default options will be used

        }
    });
    dict.addStringField(R.id.tvCarModel, new StringExtractor<Cars>() {
        @Override
        public String getStringValue(Cars product, int position) {
            return "Car Model: " + product.Car_Model;
        }
    });
    dict.addStringField(R.id.tvCarType, new StringExtractor<Cars>() {
        @Override
        public String getStringValue(Cars product, int position) {
            return "Car Type: " + product.Car_Type;
        }
    });

    dict.addStringField(R.id.tvCapacity, new StringExtractor<Cars>() {
        @Override
        public String getStringValue(Cars product, int position) {
            return "Capacity: " + product.Capacity;
        }
    });
    dict.addStringField(R.id.tvFuelType, new StringExtractor<Cars>() {
        @Override
        public String getStringValue(Cars product, int position) {
            return "Fuel Type: " + product.FuelType;
        }
    });
    dict.addStringField(R.id.tvPlateNumber, new StringExtractor<Cars>() {
        @Override
        public String getStringValue(Cars product, int position) {
            return "Plate Number: " + product.PlateNumber;
        }
    });
    dict.addStringField(R.id.tvPoster, new StringExtractor<Cars>() {
        @Override
        public String getStringValue(Cars product, int position) {
            return "Posted by: " + product.owner;
        }
    });


adapter = new FunDapter<>(
        ownerhome.this, carList, R.layout.layout_list, dict);

lvCars.setAdapter(adapter);
lvCars.setOnItemClickListener(this);

}


@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    Cars selectedCars = carList.get(position);
    Intent in = new Intent(ownerhome.this, DetailActivity.class);
    in.putExtra("cars", selectedCars);
    startActivity(in);
}
}

0 个答案:

没有答案